Release 10.1A: OpenEdge Development:
Progress 4GL Handbook
Defining a function
This is the syntax you use to define the header for a function:
A function always must explicitly return a value of the data type you name. It can take one or more parameters just as a procedure can. Although this means that a function can return
OUTPUTparameters or useINPUT-OUTPUTparameters just as a procedure can, you should consider that a function normally takes one or moreINPUTparameters, processes their values, and returns itsRETURNvalue as a result. If you find yourself defining a function that hasOUTPUTparameters, you should reconsider and probably make it a procedure instead. One of the major features and benefits of a function is that you can embed it in a larger expression and treat its return value just as you would a variable of the same type. If there areOUTPUTparameters, this won’t be the case as you must check their values in separate statements.The body of the function can contain the same kinds of statements as an internal procedure. Just as with internal procedures, you cannot define a temp-table or any shared object within the function. It has two important additional restrictions as well:
- Within a function, you cannot define any input-blocking statements, such as a
WAIT-FORstatement or any statement that prompts the user for input.- You cannot reference a function within a Progress
ASSIGNstatement or other 4GL updating statement that could cause an index to be updated. The interpreter might not be able to deal with transaction-related code inside the function itself in the middle of the update statement. It is a very good practice to avoid using functions in any code statements that update the database.A function must contain at least one
RETURNstatement to return a value of the appropriate data type:
The
return-valuecan be a constant, variable, field, or expression (including possibly even another function reference) that evaluates to the data type the function was defined to return.A function must end with an
ENDstatement. Just as an internal procedure normally ends with anEND PROCEDUREstatement, it is normal to end a function with an explicitEND FUNCTIONstatement to keep the organization of your procedure file clearer. TheFUNCTIONkeyword is optional in this case but definitely good programming practice.Although a function must always have a return type and return a value, a reference to the function is free to ignore the return value. That is, a 4GL statement can simply consist of a function reference without a return value, much like a
RUNstatement without theRUNkeyword. This might be appropriate if the function returns a true/false value indicating success or failure, and in a particular piece of code, you are not concerned with whether it succeeded or not.Whether a function takes any parameters or not, you must include parentheses, even if they’re empty, on every function reference in your executable code.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |